<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\common\controller\Admin;

class Batteryba extends Admin
{
    public function index()
    {
        $data['with'] = 'user,userpid';
        if($this->request->isPost())
        {
            $post = post();
            $where = [];
            if(isset($post['time'])){
                list($first,$last) = explode('至',$post['time']);
                $data['where'][] = ['update_time','>=',strtotime(trim($first))];
                $data['where'][] = ['update_time','<=',strtotime(trim($last))];
            }
            //租赁人
            if(isset($post['username']))
            {
                $user = model('user')->field('id')->where('username','=',$post['username'])->find();
                if(!empty($user))
                {
                    $user = $user->toArray();
                    $data['where'][] = ['user_id','=',$user['id']];
                }
            }
            //状态
            if(isset($post['status']))
            {
                $data['where'][] = ['status','=',$post['status']];
            }
        	$list = ysh_select($this->request->controller(),$data);
        }else{
            $list = ysh_select($this->request->controller(),$data);
        }
//        pr($list);
        //数据
        $this->assign('list',$list['list']['data']);
        //page
        $this->assign('page',$list['page']);
        //总个数
        $this->assign('count',$list['list']['total']);
        //显示条数
        $this->assign('limit',$list['list']['per_page']);
        //当前页数
        $this->assign('curl',$list['list']['current_page']);
        //页码数
        $this->assign('groups',$list['list']['last_page']);
        return $this->fetch();
    }
    public function add()
    {
        if($this->request->isPost())
        {
            $post = post();
            $user = model('user')->where('username','=',$post['username'])->find();
            if(!empty($user)){
                $user = $user->toArray();
                $time = time();
                $aa = model($this->request->controller())->field('id,batterybaid')->order('id','desc')->find()->toArray();
                $model = model($this->request->controller())->save([
                    'user_id'=>$user['id'],
                    'name'=>$post['name'],
                    'status'=>1,
                    'czid'=>session('login')['id'],
                    'batterybaid'=>$aa['batterybaid']*1+1,
                    'create_time'=>$time,
                    'update_time'=>$time
                ]);
                if($model){
                    $this->redirect('index');
                }else{
                    $this->assign('error','添加失败');
                }
            }else{
               $this->assign('error','账号不存在，绑定失败');
            }
        }
        return $this->fetch();
    }
    public function upload()
    {
    	$id = input('id');
    	$list = model($this->request->controller())->where('id','=',$id)->with('user,userpid,fwgl,business')->find()->toArray();
    	// pr($list);
    	$this->assign('list',$list);
    	return $this->fetch();
    }
    //分配商家
    public function upss()
    {
        if($this->request->isPost())
        {
            $post = post();
            if(!isset($post['username'])) return false;
            $list = ysh_select('user',['with'=>'business','where'=>
                [['username','=',$post['username']]]
            ],2);
            if($list['list']['business_id'] > 0){
            	$list['code'] = 200;
            }else{
            	return ['code'=>500,'username'=>''];
            }
            return $list;
        }
    }
    public function upfp()
    {
        if($this->request->isPost())
        {
            $post = post();
            //选中的商家
            $list = ysh_select('business',['where'=>[['user_id','=',$post['sid']]]],2);
            //当前机台
            $now = ysh_select('batteryba',['where'=>['id'=>$post['id']]],2);
            if($now['list']['business_id'] >0)
            {
            	//当前机台有绑定商家
            	//重新统计商家机台数量
            	$all = ysh_select('batteryba',['limit'=>0,'where'=>[['business_id','=',$now['list']['business_id']]]]);
            	$all = array_filter($all);
            	if(!empty($all)){
            		$num = count($all);
            	}else{
            		$num = 0;
            	}
            	ysh_upload('business',['id'=>$now['list']['business_id'],'num'=>$num]);
            }else{
            	//当前机台没有绑定商家
            	//重新统计商家机台数量
            	$num = $list['list']['num']*1+1;
            	ysh_upload('business',['id'=>$now['list']['business_id'],'num'=>$num]);
            }
            //把机台绑定到商家下
            $model = ysh_upload('batteryba',['id'=>$now['list']['id'],'business_id'=>$list['list']['id']]);
            return $model;
        }
    }
    //分配设备管理员
    public function upsstp()
    {
        if($this->request->isPost())
        {
            $post = post();
            if(!isset($post['username'])) return false;
            $list = ysh_select('user',['where'=>
                [['username','=',$post['username']]]
            ],2);
        	$list['code'] = 200;
            return $list;
        }
    }
    public function upfptp()
    {
        if($this->request->isPost())
        {
            $post = post();
            //选中的设备管理人
            $list = ysh_select('user',['where'=>[['id','=',$post['sid']]]],2);
            //当前机台
            $now = ysh_select('batteryba',['where'=>['id'=>$post['id']]],2);
            //当前机台是否有绑定设备管理员
            if($now['list']['pid'] > 0){
            	//有设备管理员重新绑定
            	//判断旧管理员还有其他机台管理不
		        $userall = ysh_select('batteryba',['limit'=>0,'where'=>['pid'=>$now['list']['pid']]]);
		        $userall = array_filter($userall);
		        if(!$userall){
		        	//没有其他机台管理，取消该账号设备管理员身份
		    		$model = ysh_upload('user',['id'=>$now['list']['pid'],'sbgl'=>2]);
		        }
		        //机台绑定新的设备管理员
		        $model = ysh_upload('batteryba',['id'=>$now['list']['id'],'pid'=>$post['sid']]);
		        //给该账号设备管理员身份
		        $model = ysh_upload('user',['id'=>$list['list']['id'],'sbgl'=>1]);
            }else{
            	//新增设备管理员
		        //把设备管理人绑定到机台下
		        $model = ysh_upload('batteryba',['id'=>$now['list']['id'],'pid'=>$post['sid']]);
		        $model = ysh_upload('user',['id'=>$list['list']['id'],'sbgl'=>1]);
            }
            return $model;
        }
    }
}
